package java44th.group.controller.wx.order;

import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketOrderGoods;
import java44th.group.bean.po.MarketUser;
import java44th.group.bean.wx.bo.order.WxOrderAndGoodsCommentBo;
import java44th.group.bean.wx.bo.order.WxOrderCommitBo;
import java44th.group.bean.wx.vo.order.WxOrderCommitVo;
import java44th.group.bean.wx.vo.order.WxOrderDetailVo;
import java44th.group.service.wx.order.WxOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Map;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/14 9:56
 * @Description:
 * @PACKAGE_NAME： java44th.group.controller.wx
 */

@RestController()
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @GetMapping("list")
    public BaseRespVo getOrderList(BasePageInfo basePageInfo, Integer showType) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        CommonData commonData = wxOrderService.getOrderListByShowType(basePageInfo, showType, marketUser.getId());
        return BaseRespVo.ok(commonData);
    }

    @GetMapping("detail")
    public BaseRespVo getOrderDetailById(Integer orderId) {
        WxOrderDetailVo wxOrderDetailVo = wxOrderService.getOrderDetailById(orderId);
        return BaseRespVo.ok(wxOrderDetailVo);
    }

    /**
     * 用户发起确认收货 修改订单状态
     *
     * @param
     * @return
     */
    @PostMapping("confirm")
    public BaseRespVo updateOrderStatus(@RequestBody Map<String, Integer> map) {
        //将订单状态修改为401
        boolean code = wxOrderService.updateOrderstatus(map.get("orderId"), (short) 401);
        wxOrderService.updateCommonts(map.get("orderId"));
        if (!code) {
            return BaseRespVo.falseParameter402("更新失败");
        }
        return BaseRespVo.ok();
    }

    @PostMapping("refund")
    public BaseRespVo updateRefundStatus(@RequestBody Map<String, Integer> map) {
        //用户已申请 用户申请退款 1代表用户申请退款

        boolean code = wxOrderService.updateRefundstatus(map.get("orderId"), (short) 1);
        wxOrderService.updateOrderstatus(map.get("orderId"), (short) 202);
        if (!code) {
            return BaseRespVo.falseParameter402("更新失败");
        }
        return BaseRespVo.ok();
    }

    @PostMapping("delete")
    public BaseRespVo deleteOrder(@RequestBody Map<String, Integer> map) {
        //用户已申请 用户申请退款 1代表用户申请退款
        boolean code = wxOrderService.deleteOrderById(map.get("orderId"));
        if (!code) {
            return BaseRespVo.falseParameter402("删除失败");
        }
        return BaseRespVo.ok();
    }

    @PostMapping("prepay")
    public BaseRespVo prepayOrder(@RequestBody Map<String, Integer> map) {
        //用户通过订单 id 修改订单状态
        boolean code = wxOrderService.updateOrderstatus(map.get("orderId"), (short) 201);
        if (!code) {
            return BaseRespVo.falseParameter402("删除失败");
        }
        return BaseRespVo.ok();
    }

    @PostMapping("cancel")
    public BaseRespVo cancelOrder(@RequestBody Map<String, Integer> map) {
        //用户通过订单 id 修改订单状态
        boolean code = wxOrderService.updateOrderstatus(map.get("orderId"), (short) 102);
        if (!code) {
            return BaseRespVo.falseParameter402("删除失败");
        }
        return BaseRespVo.ok();
    }

    @GetMapping("goods")
    public BaseRespVo getGoodsInfoByGidAndOidToComment(Integer orderId, Integer goodsId) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();
        MarketOrderGoods marketOrderGoods = wxOrderService.getGoodsInfoByGidAndOidToComment(orderId, goodsId, userId);
        return BaseRespVo.ok(marketOrderGoods);
    }


    @PostMapping("comment")
    public BaseRespVo orderAndGoodsComment(@RequestBody WxOrderAndGoodsCommentBo wxOrderAndGoodsCommentBo) {
        boolean code = wxOrderService.orderAndGoodsComment(wxOrderAndGoodsCommentBo);
        if (!code) {
            return BaseRespVo.falseParameter402("评论失败 你搞事情是吧");
        }
        return BaseRespVo.ok();
    }

    /**
     * 用户提交订单
     *
     * @param
     * @return
     */
    @PostMapping("submit")
    public BaseRespVo orderCommit(@RequestBody WxOrderCommitBo wxOrderCommitBo) {
        try {
            WxOrderCommitVo wxOrderCommitVo = wxOrderService.orderCommit(wxOrderCommitBo);
            if (wxOrderCommitVo == null) {
                return BaseRespVo.invalidData("订单提交錯誤");
            }
            return BaseRespVo.ok(wxOrderCommitVo);
        } catch (NullPointerException e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("请勿重复提交");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("商品库存不足");
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("订单提交錯誤");
        }
    }

}
